/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.hugo.senai.view;

import br.com.hugo.senai.entities.Conteudo;
import br.com.hugo.senai.entities.Curso;
import br.com.hugo.senai.entities.Disciplina;
import br.com.hugo.senai.dao.Hibernate;
import br.com.hugo.senai.entities.Prova;
import br.com.hugo.senai.entities.Questao;
import br.com.hugo.senai.entities.Questao_e_Prova;
import br.com.hugo.senai.entities.Questao_e_ProvaPK;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import br.com.hugo.senai.relatorio.Visualiza;

/**
 *
 * @author Simulado
 */
public class AddQuestao extends javax.swing.JFrame {

    /**
     * Creates new form AddQuestao
     */
    public AddQuestao() {
        initComponents();
        
        
           cbProva.removeAllItems();
        
        List <Prova> provas = Hibernate.listAll(Prova.class);
        
        for(Prova prova: provas){
            cbProva.addItem(prova);
        }
        
           List<Curso> cursos = Hibernate.listAll(Curso.class);

        cbQuestaoCurso.removeAllItems();

        for (Curso curso : cursos) {
            cbQuestaoCurso.addItem(curso);
        }
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cbProva = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        cbQuestaoCurso = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        cbQuestaoMateria = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        cbQuestaoConteudo = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tQuestoes = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        tfPontuacao = new javax.swing.JTextField();

        jLabel1.setText("jLabel1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Adicionar questão");

        jLabel2.setText("Prova:");

        cbProva.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel7.setText("CURSO:");

        cbQuestaoCurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbQuestaoCursoActionPerformed(evt);
            }
        });

        jLabel8.setText("DISCIPLINA:");

        cbQuestaoMateria.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cbQuestaoMateriaMouseClicked(evt);
            }
        });
        cbQuestaoMateria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbQuestaoMateriaActionPerformed(evt);
            }
        });

        jLabel9.setText("CONTEÚDO:");

        cbQuestaoConteudo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cbQuestaoConteudoMouseClicked(evt);
            }
        });
        cbQuestaoConteudo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbQuestaoConteudoActionPerformed(evt);
            }
        });

        jButton1.setText("PESQUISAR");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        tQuestoes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "enunciado", "resposta", "objetivo", "alternativa", "provao"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tQuestoes.getTableHeader().setReorderingAllowed(false);
        tQuestoes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tQuestoesMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tQuestoes);
        if (tQuestoes.getColumnModel().getColumnCount() > 0) {
            tQuestoes.getColumnModel().getColumn(0).setMinWidth(0);
            tQuestoes.getColumnModel().getColumn(0).setPreferredWidth(0);
            tQuestoes.getColumnModel().getColumn(0).setMaxWidth(0);
        }

        jButton2.setText("Adicionar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Exibir");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel3.setText("Pontuação:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbProva, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 802, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbQuestaoCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbQuestaoMateria, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbQuestaoConteudo, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton1))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton2)
                                .addGap(18, 18, 18)
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfPontuacao, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(77, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cbProva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel9)
                        .addComponent(cbQuestaoConteudo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(cbQuestaoMateria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7)
                        .addComponent(cbQuestaoCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tfPontuacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addGap(33, 33, 33))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbQuestaoCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbQuestaoCursoActionPerformed
        cbQuestaoMateria.removeAllItems();

        List<Disciplina> materias = Hibernate.listAll(Disciplina.class, (Curso) cbQuestaoCurso.getSelectedItem(), "curso");

        for (Disciplina materia : materias) {
            cbQuestaoMateria.addItem(materia);
        }
    }//GEN-LAST:event_cbQuestaoCursoActionPerformed

    private void cbQuestaoMateriaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cbQuestaoMateriaMouseClicked

    }//GEN-LAST:event_cbQuestaoMateriaMouseClicked

    private void cbQuestaoMateriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbQuestaoMateriaActionPerformed
        cbQuestaoConteudo.removeAllItems();

        List<Conteudo> conteudos = Hibernate.listAll(Conteudo.class, (Disciplina) cbQuestaoMateria.getSelectedItem(), "disciplina");

        for (Conteudo conteudo : conteudos) {
            cbQuestaoConteudo.addItem(conteudo);
        }
    }//GEN-LAST:event_cbQuestaoMateriaActionPerformed

    private void cbQuestaoConteudoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cbQuestaoConteudoMouseClicked

    }//GEN-LAST:event_cbQuestaoConteudoMouseClicked

    private void cbQuestaoConteudoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbQuestaoConteudoActionPerformed

    }//GEN-LAST:event_cbQuestaoConteudoActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //List<Questao> questoes = Hibernate.listAll(Questao.class, (Conteudo) cbQuestaoConteudo.getSelectedItem(), "conteudo");
        Conteudo c = (Conteudo) cbQuestaoConteudo.getSelectedItem();

        List <Questao> questoes = Hibernate.listByQuestion(c.getId());
        DefaultTableModel tableModel = (DefaultTableModel) tQuestoes.getModel();

        tableModel.setNumRows(0);

        for (Questao questao : questoes) {
            tableModel.addRow(new Object[]{questao.getId(), questao.getEnunciado(),
                questao.getResposta(), questao.getObjetivo(), questao.isAlternativa(), questao.isProvao()});
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void tQuestoesMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tQuestoesMouseReleased

//        if (evt.isPopupTrigger() && tQuestoes.getSelectedRowCount() > 0) {
//            tQuestoes.setColumnSelectionInterval(0, 4);
//
//            pmTabela.show(evt.getComponent(), evt.getX(), evt.getY());
//        }
    }//GEN-LAST:event_tQuestoesMouseReleased

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       
       Questao_e_Prova qp = new Questao_e_Prova();
       
       qp.setProva((Prova) cbProva.getSelectedItem());
       
       Questao q = (Questao) Hibernate.get(Questao.class, (int) tQuestoes.getValueAt(tQuestoes.getSelectedRow(), 0));
       qp.setQuestao(q);
       qp.setPontuacao(Integer.parseInt(tfPontuacao.getText()));
     
       
       Hibernate.save(qp);
     
       
       
        
        
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
      
        
        Prova p = (Prova) cbProva.getSelectedItem();
        Visualiza v;
        try {
            v = new Visualiza();
              v.visualizar(p.getId());
            
        } catch (Exception ex) {
            Logger.getLogger(AddQuestao.class.getName()).log(Level.SEVERE, null, ex);
        }
       
     
        
        
        
        
        
        
    }//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AddQuestao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AddQuestao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AddQuestao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AddQuestao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AddQuestao().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbProva;
    private javax.swing.JComboBox cbQuestaoConteudo;
    private javax.swing.JComboBox cbQuestaoCurso;
    private javax.swing.JComboBox cbQuestaoMateria;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tQuestoes;
    private javax.swing.JTextField tfPontuacao;
    // End of variables declaration//GEN-END:variables
}
